#include<stdio.h>
#include<pwd.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#define LEN 1024
#define NUM 32


int main()
{
  char cmd[LEN];
  char* myargv[NUM];
  char hostname[32];
  char pwd[128];
  while(1)
  {
    struct passwd* pass = getpwuid(getuid());
    gethostname(hostname, sizeof(hostname) -1);
    getcwd(pwd, sizeof(pwd) - 1);
    int len = strlen(pwd);
    char* p = pwd + len - 1;
    while(*p != '/')
    {
      --p;
    }
    ++p;
    printf("[%s@%s %s]$ ", pass->pw_name, hostname, p);
    fgets(cmd, LEN, stdin);
    cmd[strlen(cmd) -1] = '\0';
    myargv[0] = strtok(cmd, " ");
    int i = 1;
    while(myargv[i] = strtok(NULL, " ")){
      ++i;
    }
    pid_t id = fork();
    if(id == 0){
      execvp(myargv[0], myargv);
      exit(1);
    }

    int status = 0;
    pid_t ret = waitpid(id, &status, 0);
    if(ret > 0){
      printf("exit code:%d\n", WEXITSTATUS(status));
    }
  }

  return 0;

}
